package com.intel.factorytest.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Looper;
import android.os.Handler;
import android.os.Message;
import android.media.AudioManager;
import android.net.Uri;
import android.media.MediaPlayer;
import android.content.Context;
import java.io.IOException;
import com.intel.factorytest.util.LogUtils;
import com.intel.factorytest.R;

public class SpeakerKhz extends BaseActivity  {
    private static final String TAG = "SpeakerKhz";
    
    Context mContext;
    private MediaPlayer mMediaPlayer;
    AudioManager audioManager;
    private static boolean mIsComplete = true;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.loge("onCreate");
        setContentView(R.layout.speaker_1khz);
        mContext = getApplicationContext();        
        initView();
        mMediaPlayer = new MediaPlayer();
        audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        audioManager.setMode(AudioManager.MODE_NORMAL);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(mIsComplete) {
            startSpeaker(mContext);
        }else {
            stopSpeaker();
        }
    }

    public void startSpeaker(Context context){
        LogUtils.logi("startSpeaker start");
        Uri uri = Uri.parse("android.resource://com.intel.factorytest/" + R.raw.test_1khz);
        LogUtils.logi("startSpeaker uri = " + uri);
        try {
            LogUtils.logi( "startSpeaker mMediaPlayer.isPlaying() = " + mMediaPlayer.isPlaying());
            mMediaPlayer.setDataSource(context, uri);
            mMediaPlayer.setLooping(true);
            if (mMediaPlayer.isPlaying()){
                mMediaPlayer.stop();
            }            
            mMediaPlayer.prepare();
            mMediaPlayer.start();
            //mMediaPlayer.setVolume(1f, 1f);
            mIsComplete = false;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

    public void stopSpeaker(){
        try {
            if(mMediaPlayer != null){                
                LogUtils.logi( "stopSpeaker mMediaPlayer.isPlaying() = " + mMediaPlayer.isPlaying());
                if(mMediaPlayer.isPlaying())
                {
                    mMediaPlayer.stop();
                    mMediaPlayer.prepare();
                }
                mMediaPlayer.release();
                mMediaPlayer = null;
                mIsComplete = true;
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onStop() {
        LogUtils.loge("onStop");
        super.onStop();
        stopSpeaker();
    }
     
    @Override
    protected void onDestroy() {
        LogUtils.loge("onDestroy");
        if (mMediaPlayer!= null) {
            mMediaPlayer = null;
        }
        super.onDestroy();
    }
    
    @Override
    public void finish() {
        LogUtils.loge("finish");
        tc.setTestResult(is_pass);
        super.finish();
    }

}
